package com.hwd.srb.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 */
public final class MD5 {

    public static void main(String[] args) {
        byte[] bytes = {-27, -68, -96, -28, -72, -119};
        byte[] bytes1 = {97, 93, -75, 122, -93, 20, 82, -102, -86, 15, -66, -107, -77, -23, 91, -45};

        System.out.println(new String(bytes));
        System.out.println(new String(bytes1));
        System.out.println(encrypt("张三"));
    }

    public static String encrypt(String strSrc) {
        try {
            char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            byte[] bytes = strSrc.getBytes();// 把明码字符串转为byte型数组
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();// 加密后获得长度为16的byte型数组
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (byte b : bytes) {
                chars[k++] = hexChars[b >>> 4 & 0xf];// 取高四位
                chars[k++] = hexChars[b & 0xf];// 取低四位
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }


}
